package com.atguigu.product.controller;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

////import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.entity.CategoryEntity;
import com.atguigu.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品三级分类
 *
 * @author dengjia
 * @email ${email}
 * @date 2022-01-05 12:03:58
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService CategoryService;

    /**
     * 树形结构列表
     */
    @RequestMapping("/list/tree")
    ////@RequiresPermissions("product:category:list")
    public R listTree(){
        //查出所有商品分类
        List<CategoryEntity> CategoryTree = CategoryService.listTree();
        //构建菜单树
        Collection<CategoryEntity> leavel1Menus = CategoryTree.stream().filter(
                CategoryEntity -> CategoryEntity.getParentCid() == 0
        )       //设置父子关系
                .map((menu) ->{
                    menu.setChildren(getChildrens(menu,CategoryTree));
                    return menu;
                })
                .sorted((menu1,menu2) -> (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort())) //排序
                .collect(Collectors.toList());

        return R.ok().put("data", leavel1Menus);
    }

    private List<CategoryEntity> getChildrens(CategoryEntity menu, List<CategoryEntity> categoryTree) {
        List<CategoryEntity> list = categoryTree.stream().filter(
                category -> category.getParentCid() == menu.getCatId()
        )
                .map(
                        category -> {
                            category.setChildren(getChildrens(category,categoryTree));
                            return category;
                        }
                )
                .sorted((menu1,menu2) -> (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort()))
                .collect(Collectors.toList());
        return list;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    ////@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = CategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    ////@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = CategoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity Category){
		CategoryService.save(Category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    ////@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity Category){
		CategoryService.updateDetail(Category);

        return R.ok();
    }

    /**
     * 修改排序
     */
    @RequestMapping("/updateSort")
    ////@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] Category){
        CategoryService.updateBatchById(Arrays.asList(Category));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    ////@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //是否
		CategoryService.removeByMenuIds(Arrays.asList(catIds));
        return R.ok();
    }

}
